<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/16
 * Time: 21:00
 */
namespace Pay\Controller;

use Common\Common\Api\Order;
use Common\Common\Api\Ordermember;
use Common\Common\Api\Redis\Channel;
use Common\Common\Api\Redis\Product;

/**
 * Class ManyiController
 * @package Pay\Controller
 * 联网支付2 ajie
 */
class Lianwang2Controller extends PayController{
    protected $config;
    protected $class;
    public function __construct(){
        parent::__construct();
        $classes = explode("\\",__CLASS__);
        $class = end($classes);
        $this->class = substr($class, 0, stripos($class, "Controller"));
    }

    public function pay($array){
        $this->config = Channel::getInstance()->getById($array['channel']);

        if(!$this->config){
            $this->showmessage('通道错误!',['code'=>C("THIRDCODES.".$this->class)]);
        }

        $parameter = array(
            'code' => $this->config['code'], // 通道名称
            'title' => $this->config['title'],
            'exchange' => 1, // 金额比例
            'gateway' => $this->config['gateway'],
            'orderid' => createOrderId(),
            'out_trade_id' => I("request.ordNo") ? I("request.ordNo") : I("request.pay_orderid"),
            'body' => 'pay',
            'channel'=>$array
        );

        // $isMobile = isMobile();
        $return = $this->orderadd( $parameter );

        switch($this->config['paytype']){
            //微信
            case 1:
            //微信H5
            case 2:
                $pay_bankcode = 'WXPAY';
                break;
            default:
                $this->showmessage('暂不支持此方式!',['code'=>C("THIRDCODES.".$this->class)]);
        }

        $data = array(
            "pay_memberid" => $this->config['mch_id'],
            "pay_orderid" => $return['orderid'],
            "pay_amount" => $return['amount'],
            "pay_applydate" => date("Y-m-d H:i:s",time()),
            "pay_bankcode" => $pay_bankcode,
            "pay_notifyurl" => $return['notifyurl'],
            "pay_callbackurl" => $return['callbackurl']
        );
        ksort($data);
        $md5str = '';
        foreach($data as $key => $val) {
            if(!empty($val)) {
                $md5str .= $key . '=' . $val . '&';
            }
        }

        $data['pay_md5sign'] = strtoupper(md5($md5str . "key=" . $this->config['signkey']));

        $data['pay_tongdao'] = 'Pawxsm';
        $data['pay_productname'] = '备注';
        $data['pay_reserved1'] = md5('wlf'.$array['userid']);

        //$res = curl($this->config['gateway'], httpstr($data));

        //echo '提交地址：'.$this->config['gateway'] . "<br/>";
        //echo '<pre/>'; print_r($data); print_r($res); exit;

        po($this->config['gateway'], $data);

    }






    //通知
    public function notifyurl(){
          //$this->debug_callback();

         $p = I("get.");

        //IP白名单
        if(!$this->checkip(getIP(),['129.204.205.58'])){
           // exit("wrong");
        }

        if(!$p || empty($p)){
            exit("wrong");
        }



        //获取订单信息
        $orderid = $p['orderid'];
        //获取订单
        $find = $this->checkOrderNotify($orderid);
        if(!$find){
            echo 'order_not_found'; exit;
        }

        $real_amount =  $p['amount'];     //实际支付金额
        $pay_amount =  $find['pay_amount']; //商户提交订单金额

        if(abs($real_amount - $pay_amount) > 1){
            echo 'wrong_money'; exit;
        }


        //获取渠道信息
        $this->config = Channel::getInstance()->find(['code'=>$this->class]);

        $sign = $p['sign'];
        unset($p['sign']);
        $ReturnArray = array( // 返回字段
            "memberid" => $p["memberid"], // 商户ID
            "orderid" =>  $p["orderid"], // 订单号
            "datetime" =>  $p["datetime"], // 交易时间
            "amount" =>  $p["amount"], // 交易金额
            "returncode" => $p["returncode"]
        );

        $key = $this->config['signkey'];

        ksort($ReturnArray);

        $str = "";
        foreach ($ReturnArray as $k => $v) {
            $str .= $k."=".$v."&";
        }
        $str .= "key=".$key;


        $sign2 = strtoupper(md5($str));

        if($sign != $sign2){
            exit("sign_wrong");
        }
        if($p['returncode'] !== '00'){
            exit("pay_fail");
        }

        $this->async($find['pay_orderid'],'ok');
    }



    public function callBackUrl(){

        echo '处理成功'; exit;
    }
}
